/*
 * Copyright (c) 2015 elion. All Rights Reserved.
 */
package com.guoyu.project.common.utils.http;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: xblibo Date: 15-6-4 Time: 下午9:28
 */
public class HttpClientUtils {

    private final static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);

    /**
     * oa验证用户名密码client
     */
    public static HttpClient oaClient = createClient(2000, 10000, 10, 10);

    /**
     * 创建代httpClient
     * 
     * @param connTimeout
     * @param readTimeout
     * @param maxTotal
     * @param MaxPerRoute
     * @return
     */
    public static HttpClient createClient(int connTimeout, int readTimeout, int maxTotal, int MaxPerRoute) {
        // 设置连接数
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setMaxConnPerRoute(maxTotal).setMaxConnTotal(MaxPerRoute);

        // 设置默认的请求超时时间
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connTimeout)
                .setConnectTimeout(connTimeout).setSocketTimeout(readTimeout).build();
        builder.setDefaultRequestConfig(requestConfig);

        builder.setHostnameVerifier(new TrustAnyHostnameVerifier());

        builder.setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16");

        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(
                10, TimeUnit.MINUTES);
        builder.setConnectionManager(poolingHttpClientConnectionManager);

        try {
            // 设置ssl
            SSLContext sslcontext = SSLContext.getInstance("SSL");
            sslcontext.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
            builder.setSslcontext(sslcontext);
        } catch (Exception e) {
            logger.error("init ssl httpClient fail", e);
        }

        return builder.build();
    }

    public static void main(String[] args) {
        HttpGet httpGet = new HttpGet(
                "http://oa.elion.com.cn/seeyon/login/proxy?login.username=haoyu1&login.password=haoyu18647761609&UserAgentFrom=mobile&login.destination=/seeyon/mob.do?method=loginTransfer");
        try {
            HttpResponse response = HttpClientUtils.oaClient.execute(httpGet);

            HttpEntity httpEntity = response.getEntity();
            String result = EntityUtils.toString(httpEntity);
            System.out.println(result);

            // 退出登录
            httpGet = new HttpGet("http://oa.elion.com.cn/seeyon/login/logout?f=m");
            HttpClientUtils.oaClient.execute(httpGet);
        } catch (IOException e) {
            logger.error("验证oa账号异常,账号名:{}", "", e);
        }
    }
}
